﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using MyCommerce.Common.CommerceClient.Security;

namespace MyCommerce.Common.CommerceClient
{
    static class Program
    {
        private static LoginForm loginForm = null;
        private static MainClient clientBase = null;
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            Infragistics.Win.AppStyling.StyleManager.Load("DefaultTheme.isl");
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);

            loginForm = new LoginForm();
            DialogResult resultLogin = loginForm.ShowDialog();
            LoadMainClient(resultLogin);
        }

        private static void LoadMainClient(DialogResult resultLogin)
        {
            if (resultLogin == DialogResult.OK)
            {
                clientBase = new MainClient(loginForm.ColCommandsAvailable);
                clientBase.mustCLoseDirect = false;
                clientBase.OnBlocking += new EventHandler(clientBase_OnBlocking);
                clientBase.OnClosingSession += new EventHandler(clientBase_OnClosingSession);

                clientBase.ObjUserLoged = loginForm.ObjUserLoged;
                clientBase.Context.UserId = loginForm.ObjUserLoged.IdUser;
                clientBase.Context.EmployeeId = loginForm.ObjUserLoged.IdEmployee;
                clientBase.Context.DealerStoreId = loginForm.ObjUserLoged.IdDealerStore;
                clientBase.Context.StoreId = loginForm.ObjUserLoged.IdStore;                
                loginForm.Dispose();
                clientBase.Context.ParentForm = clientBase;
                clientBase.ShowDialog();
            }
            //else if (resultLogin == DialogResult.Abort)
            //    MessageBox.Show("Usuario o contraseña incorrectos.", "Login", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }

        static void clientBase_OnClosingSession(object sender, EventArgs e)
        {
            clientBase.mustCLoseDirect = true;
            clientBase.Dispose();
            loginForm = new LoginForm();
            DialogResult resultLogin = loginForm.ShowDialog();
            LoadMainClient(resultLogin);
        }

        static void clientBase_OnBlocking(object sender, EventArgs e)
        {
            clientBase.Hide();
            loginForm = new LoginForm(clientBase.ObjUserLoged);
            loginForm.OnCloseAll += new EventHandler(loginForm_OnCloseAll);
            DialogResult resultLogin = loginForm.ShowDialog();
            if (resultLogin == DialogResult.OK)
                clientBase.Show();
        }

        static void loginForm_OnCloseAll(object sender, EventArgs e)
        {
            clientBase.mustCLoseDirect = true;
            clientBase.Dispose();
            loginForm.Dispose();
        }
    }
}
